#!/usr/bin/env php
<?php
/**
* This autoload stuff is a hot mess, and I don't like it, but it works so I don't wanna touch it.
*/
if (Phar::running()==''){
// if global scrawl is called & scrawl is also installed to the current package via composer
// then only run the package-level install
$vendor_install = getcwd().'/vendor/bin/scrawl';
//vendor_install_2 accounts for the composer update that stopped symlinking & started include-ing the target bin script
$vendor_install2 = getcwd().'/vendor/taeluf/code-scrawl/bin/scrawl';
if (file_exists($vendor_install)
&&realpath(__FILE__)!=realpath($vendor_install)
&&realpath(__FILE__)!=realpath($vendor_install2)
){
$args = array_slice($argv,1);
$args = array_filter($args, function($v){
return '"'.addslashes($v).'"';
});
$cmd = "$vendor_install ". implode(' ',$args);
// because composer started using an include() instead of symlink, the #!/shebang line was occuring before `namespace Composer` & causing errors
// so now we use `passthru` here to execute the vendor script rather than include it
passthru("$vendor_install ". implode(' ',$args));
return;
}
// require() the current package's autoloader
$cwd_autoload = getcwd().'/vendor/autoload.php';
if (!is_file($cwd_autoload)){
echo "\n\nAutoloader file '$cwd_autoload' not found. ";
} else {
require($cwd_autoload);
}
//require own global autoloader
$own_global_autoload = dirname(__DIR__).'/vendor/autoload.php';
if (is_file($own_global_autoload)&&realpath($cwd_autoload)!=realpath($own_global_autoload)){
require($own_global_autoload);
}
} else {
require(__DIR__.'/../vendor/autoload.php');
}
$dir = getcwd();
$cli = new \Tlf\Cli();
$cli->load_inputs(json_decode(file_get_contents(dirname(__DIR__).'/src/defaults.json'),true));
$config_locations = [
// NOTE: IF CHANGING THIS, update the README and Configurations docs
'config/scrawl.json',
'.config/scrawl.json',
'.docsrc/scrawl.json',
'scrawl.json'
];
foreach ($config_locations as $rel_path){
if (is_file($abs_path = $dir.'/'.$rel_path)){
$cli->load_inputs(json_decode(file_get_contents($abs_path),true));
break;
}
}
$cli->load_stdin();
$config_name_conversions = [
// key becomes value
// NEW config names, what I want, converting to old config names so I don't have to update code.
//'api.dirs' => 'dir.scan',
'dir.templates' => 'template.dirs',
'warning.overwriteRootREADME' => 'readme.copyFromDocs',
'warning.deleteExistingDocs' => 'deleteExistingDocs',
'scrawl.extensions'=>'ScrawlExtensions',
'markdown.fixNewLines'=>'markdown.preserveNewLines',
'markdown.prependNOEDIT' => 'markdown.prependGenNotice',
// Old config names converting to coded config names, for backward compatability
'dir.code' => 'dir.scan',
'dir.template' => 'dir.src',
'dir.template_files' => 'template.dirs',
];
foreach ($config_name_conversions as $configured_key => $target_key){
if (isset($cli->args[$configured_key]) && !isset($cli->args[$target_key])){
$cli->args[$target_key] = $cli->args[$configured_key];
unset($cli->args[$configured_key]);
}
}
if (!isset($cli->args['dir.root']))$cli->args['dir.root'] = getcwd();
$string_to_array_conversions = [
'dir.scan',
'template.dirs',
'ScrawlExtensions',
];
foreach ($string_to_array_conversions as $key){
if (isset($cli->args[$key]) && is_string($cli->args[$key])){
$cli->args[$key] = [$cli->args[$key]];
}
}
/////////
// prepend `dir.root` to these path configs
/////////
$root_prefix = ['dir.src', 'dir.docs', 'template.dirs', 'file.bootstrap'];
foreach ($root_prefix as $key){
$arg = $cli->args[$key] ?? null;
if ($arg==null)continue;
if (is_array($arg)){
$cli->args[$key] = array_map(function($path) use ($cli){
return $cli->args['dir.root'].'/'.$path;
},$cli->args[$key]);
} else {
$cli->args[$key] = $cli->args['dir.root'].'/'.$arg;
}
}
////////
// init scrawl & run it
////////
$scrawl = new \Tlf\Scrawl($cli->args);
$cli->load_command('main', [$scrawl, 'run'], "Generate Documentation");
/**
* Get absolute path to the generated documentation file of a code file.
*
* @usage `scrawl get_doc_path "/absolute/path.php"
* @arg absolute path to document
*/
$cli->load_command('get_doc_path', [$scrawl, 'get_doc_path'], "Get absolute path to the generated documentation file of a code file.");
$cli->load_command('get_doc_src_path', [$scrawl, 'get_doc_source_path'], "Get absolute path to the Editable .src.md file of a .md file.");
// $cli->load_command('init', [$scrawl, 'run_init']);
// $runner->backward_compatability();
return $cli->execute();